<?php

class HomeController extends BaseController
{
    /*
      |--------------------------------------------------------------------------
      | Default Home Controller
      |--------------------------------------------------------------------------
      |
      | You may wish to use controllers instead of, or in addition to, Closure
      | based routes. That's great! Here is an example controller method to
      | get you started. To route to this controller, just add the route:
      |
      |	Route::get('/', 'HomeController@showWelcome');
      |
     */

    protected $layout = 'layout';

    public function showWelcome()
    {
        return View::make( 'hello' );

    }

    public function index()
    {


        $this->layout->content = View::make( 'index' );

    }

    public function ola()
    {
        $teste = 'teste';
        //$this->layout->content = View::make( 'olamundo' )->with( 'teste', $teste );
        return Response::json( array('msg' => 'Teste') );

    }

    public function login()
    {

        $rules = array(
            'login' => 'required', // make sure the email is an actual email
            'senha' => 'required|min:6' // password can only be alphanumeric and has to be greater than 3 characters
        );

        // run the validation rules on the inputs from the form
        $validator = Validator::make( Input::all(), $rules );

        if ($validator->fails()) {
            //return Redirect::to( '/' )
            //                ->withErrors( $validator ) // send back all errors to the login form
            //                ->withInput( Input::except( 'senha' ) ); // send back the input (not the password) so that we can repopulate the form
            //$validator->
        } else {

            // create our user data for the authentication
            $userdata = array(
                'login'    => Input::get( 'login' ),
                'password' => Input::get( 'senha' )
            );


            $user = Usuario::where( 'login', Input::get( 'login' ) )->first();
            if ($user && $user->senha == md5( Input::get( 'senha' ) )) {
                Auth::login( $user );
            } else {
                throw new \Exception( 'Usuário ou senha incorretas.' );
            }
        }


        return Response::json( array('type' => 'success') );

    }

}
